package net.huashitong.Indicators.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.paas.api.common.util.HttpClientUtil;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.requestModel.DocumentsModel;
import net.huashitong.until.Until;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "app/analysis")
public class AnalysisController extends JsonBaseController {
    public static final String SITE_ID = "155";
    public static final String SECRET = "WCM48785";
    private Logger log  = LoggerFactory.getLogger(AnalysisController.class);
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 获取所有栏目
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allchannels", method = RequestMethod.POST)
    public Message getAllChannels() {
        String accessToken = getAccessToken();
        if (StringUtils.isNotBlank(accessToken)) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("pId", "0");
            parameters.put("accessToken", accessToken);
            // 发起https请求.
            String responseStr = HttpClientUtil.httpPost(Until.GET_CHS, parameters);
            JSONObject jsStr = JSONObject.parseObject(responseStr);
            String success = jsStr.getString("success");
            if ("true".equals(success)) {
                JSONArray data = jsStr.getJSONArray("rows");
                return coverMessage("200", "获取所有栏目", data);
            } else {
                return coverMessage("501", "获取栏目失败", null);
            }
        } else {
            return coverMessage("501", "获取accessToken失败", null);
        }
    }

    /**
     * 获取指定栏目接口
     *
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getchannel", method = RequestMethod.POST)
    public Message getChannel(@RequestBody DocumentsModel model) {
        String accessToken = getAccessToken();
        if (StringUtils.isNotBlank(accessToken)) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("chnlId", model.getChnlId());
            parameters.put("accessToken", accessToken);
            // 发起https请求.
            String responseStr = HttpClientUtil.httpPost(Until.GET_CH, parameters);
            JSONObject jsStr = JSONObject.parseObject(responseStr);
            String success = jsStr.getString("success");
            if ("true".equals(success)) {
                JSONObject data = jsStr.getJSONObject("data");
                return coverMessage("200", "获取指定栏目属性信息成功", data);
            } else {
                return coverMessage("501", "获取指定栏目属性信息失败", null);
            }
        } else {
            return coverMessage("501", "获取accessToken失败", null);
        }

    }

    /**
     * 获取文章列表
     *
     * @param model date、pageNo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getdocuments", method = RequestMethod.POST)
    public Message getDocuments(@RequestBody DocumentsModel model) {
        String accessToken = getAccessToken();
        if (!"false".equals(accessToken)) {
            String params = "";
            if (StringUtils.isNotBlank(model.getDate())) {
                String date = model.getDate();
                params = "%7b%27CHNLIDS%27%3a%2780600%27%2c%27SDATE%27%3a%27" + date + "-01-01+00%3a00%3a00%27%2c%27EDATE%27%3a%27" + date + "-12-31+23%3a59%3a59%27%7d";
            } else {
                String date = Until.getCurrenYear();
                params = "%7b%27CHNLIDS%27%3a%2780600%27%2c%27SDATE%27%3a%27" + date + "-01-01+00%3a00%3a00%27%2c%27EDATE%27%3a%27" + date + "-12-31+23%3a59%3a59%27%7d";
            }
            String url = "pageNo="+model.getPageNo()+"&accessToken=" + accessToken + "&pageSize=20&params=" + params;
            String response = null;
            try {
                response = Until.HttpGET(Until.GET_DOCUMENTS + url, 15000, 15000);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(Until.GET_DOCUMENTS + url);
            }
            JSONObject jsStr = JSONObject.parseObject(response);
            String success = jsStr.getString("success");

            if ("true".equals(success)) {
                String dateTime = Until.getCurrenYear();
                Map<String, Object> map = new HashMap<>();
                JSONArray data = jsStr.getJSONArray("rows");

                map.put("dateTime", dateTime);
                map.put("data", data);
                System.out.println("程序结束时间：" + Until.getCurrentDateTime());

                return coverMessage("200", "获取成功", map);
//                return coverMessage("200", "获取成功", data);
            } else {
                return coverMessage("501", "获取失败", null);
            }
        } else {
            return coverMessage("501", "获取accessToken失败", null);
        }
    }

    /**
     * 获取指定文章接口
     *
     * @param model docId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getarticle", method = RequestMethod.POST)
    public Message getArticle(@RequestBody DocumentsModel model) {
        String accessToken = getAccessToken();
        if (StringUtils.isNotBlank(accessToken)) {
            Map<String, Object> parameters = new HashMap<>();
            String params = "%7b%27EXTFIELDS%27%3a%5b%27IDXID%27%2c%27FILENO%27%2c%27PUBDATE%27%2c%27PUBORG%e2%80%99%5d%7d";
            String url = "docId=" + model.getDocId() + "&accessToken=" + accessToken + "&" + params;
            String response = null;
            try {
                response = Until.HttpGET(Until.GET_ARTICLE + url, 15000, 15000);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(Until.GET_ARTICLE + url);
            }
            JSONObject jsStr = JSONObject.parseObject(response);

            String success = jsStr.getString("success");
            if ("true".equals(success)) {
                JSONObject data = jsStr.getJSONObject("data");
                return coverMessage("200", "获取成功", data);
            } else {
                return coverMessage("501", "获取失败", null);
            }
        } else {
            return coverMessage("501", "获取accessToken失败", null);
        }
    }

    public String getAccessToken() {
        String response = null;
        String url = "siteId=155&secret=WCM48785";
        try {
            response = Until.HttpGET(Until.GET_ACCESS_TOKEN + url, 15000, 15000);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(Until.GET_ACCESS_TOKEN + url);
        }
        log.info("lanaly :"+response);
        JSONObject jsStr = JSONObject.parseObject(response);
        String success = jsStr.getString("success");
        if ("true".equals(success)) {
            return jsStr.getString("accessToken");
        } else {
            return "false";
        }
    }
}
